package com.example.demo.service;


import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegistService {
    @Autowired
    private UserMapper userMapper;

    public int registUser(String username,String password,String email,int role,int state){
        //  return userDao.registUser(username,password,email,state);
        if (userMapper.usernameIfExist(username) == null){
            //需要查找一下用户名是否重复吗？,成功返回1
           if(userMapper.registUser(username,password,email,role,state) == 1){
               //成功
               return 0;
           }else{
               //sql语句错误
               return 1;
           }
        }else
            return 2;//名字重复
    }

    public boolean checkCodeIF(String codeEmail, String codeInput) {
        return  codeEmail.equals(codeInput);
    }

    public boolean checkPasswordIF(String password, String repassword) {
        return password.equals(repassword);
    }

    public String check(String username,  String password,String repassword,  String email,String codeInput ,String codeEmail){
        if (checkCodeIF(codeEmail, codeInput)) {
            if (checkPasswordIF(password, repassword)) {
                int flag = registUser(username, password, email, 1,0);
                if (flag == 0) {
                    //成功
                    return "<script type='text/javascript'>alert('注册成功，请登录使用本系统');window.location='/login'</script>";
                } else if (flag == 1) {
                    //sql语句出错
                    return "<script type='text/javascript'>alert('注册失败，sql语句出错');window.location='/register'</script>";
                } else {
                    //用户名重复，注册失败
                    return "<script type='text/javascript'>alert('注册失败，用户名重复，请重新输入');window.location='/register'</script>";
                }
            }else{
                return "<script type='text/javascript'>alert('注册失败，密码两次输入不一样，请重新输入');window.location='/register'</script>";
            }
        }else{
            return "<script type='text/javascript'>alert('验证码错误，请重新输入');window.location='/register'</script>";
        }
    }
}
